package com.ternnetwork.toolkit.controller.campaign;

import java.io.OutputStream;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.commons.utils.ResponseContentType;
import com.ternnetwork.persistence.page.BootstrapGrid;
import com.ternnetwork.persistence.page.Page;
import com.ternnetwork.toolkit.model.campaign.CampaignDataRequest;
import com.ternnetwork.toolkit.service.campaign.CampaignChannelService;
import com.ternnetwork.toolkit.service.campaign.CampaignDataService;
import com.ternnetwork.toolkit.service.campaign.CampaignService;


@Controller@Scope("prototype")
@RequestMapping("/toolkit/campaign/data/*")
public class CampaignDataController {
	
	@Resource
	private CampaignDataService campaignDataService;
	
	@Resource
	private CampaignChannelService campaignChannelService;
	
	@Resource
	private CampaignService campaignService;
	
	
	@RequestMapping("manage.htm")
	public ModelAndView manage(){
		return new ModelAndView("/WEB-INF/view/toolkit/campaign/data_manage.jsp").addObject("campaignList", campaignService.findAll()).addObject("channelList", campaignChannelService.findAll());
	}
	
	
	@RequestMapping(name="add.htm",method=RequestMethod.GET)
	public ModelAndView add(){
		return new ModelAndView("/WEB-INF/view/toolkit/campaign/data_add.jsp").addObject("campaignList", campaignService.findAll()).addObject("channelList", campaignChannelService.findAll());
	}
	
	
	@RequestMapping(name="add.htm",method=RequestMethod.POST)
	public @ResponseBody BaseResponse add(HttpServletResponse response,String jsonStr){
		CampaignDataRequest infoCollectionDataRequest=JsonUtils.jsonToObject(jsonStr, CampaignDataRequest.class);
		return campaignDataService.idoAdd(infoCollectionDataRequest);
	}
	
	@RequestMapping("delete.htm")
	public @ResponseBody BaseResponse delete(HttpServletResponse response,Long id){
		return campaignDataService.idoDelete(id);
	}
	
	@RequestMapping("query.htm")
	public @ResponseBody BootstrapGrid query(HttpServletResponse response,String sort,String order,int limit, int offset,String campaignId,String channelId,String startTime,String endTime,String buyerMobilePhoneNo,String potentialBuyerMobilePhoneNo){
		Page page=new Page();
		if (offset > 0) {
			offset = offset/limit;
		}
		page.setPageNo(offset);
		page.setPageSize(limit);
	    return campaignDataService.queryToBootstrapGrid(campaignId, channelId, startTime, endTime, buyerMobilePhoneNo, potentialBuyerMobilePhoneNo, page);
	}
	
	//跨域调用
	@RequestMapping("addJSONP.htm")
	public void addJSONP(PrintWriter out,HttpServletRequest request,HttpServletResponse response,String jsonpcallback,String jsonStr){
	       	response.setContentType("text/javascript");//如果不这样设置 chrome浏览器无法调用
			CampaignDataRequest infoCollectionDataRequest=JsonUtils.jsonToObject(jsonStr, CampaignDataRequest.class);
			String result=campaignDataService.idoAdd(infoCollectionDataRequest).toString();
	        out.print(jsonpcallback+"("+result+")");
   }
	
	

	
	    @RequestMapping("exportExcel.htm")
		public void exportExcel(HttpServletRequest request,HttpServletResponse response,String campaignId,String channelId,String startTime,String endTime,String buyerMobilePhoneNo,String potentialBuyerMobilePhoneNo) {
				try {
				OutputStream  os= response.getOutputStream();
	            response.reset();
				String contentType=ResponseContentType.getContentTypeByFileExtendsName("xls");
				response.setHeader("Content-Disposition","attachment; filename=info_collection_data.xls");
				response.setContentType(contentType);
				campaignDataService.exportExcel(campaignId, channelId, startTime, endTime, buyerMobilePhoneNo, potentialBuyerMobilePhoneNo).write(os);
				os.flush();
				os.close();  
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	    
	    
	    
		@RequestMapping(method=RequestMethod.POST,path="upload.htm")
		public @ResponseBody BaseResponse upload(@RequestParam MultipartFile[] file,String currentDir,String dir,String imgWidth,String imgHeight,String align,String imgTitle,String localUrl,HttpServletResponse response,HttpServletRequest request){
			    return campaignDataService.upload(file, currentDir, dir,request);
		}

}
